/**
 * Created by Administrator on 2017/6/19 0019.
 */

var gulp = require("gulp");
var browserSync = require("browser-sync").create();
var plumber = require("gulp-plumber");
var cleanCss = require("gulp-clean-css");
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
var sass = require("gulp-sass");

var concat = require("gulp-concat");

//服务器
gulp.task("serve",function () {
    browserSync.init({
        server:{
            baseDir:"www/"
        }
    });
});

//刷新浏览器
gulp.task("refresh",function () {
    browserSync.reload();
});

//html, css, js, sass 事物
gulp.task("html",function () {
    gulp.src("./src/*.html")
        .pipe(gulp.dest("./www/"));
});

gulp.task("minify-css",function () {
    gulp.src("./src/css/**/*.css")
        .pipe(plumber())
        .pipe(cleanCss({ compatibility:"ie8" })) //兼容性
        .pipe(rename({ suffix: ".min"}))
        .pipe(gulp.dest("./www/css/"));
});


// 发布测试地址的时候要注释掉
gulp.task("minify-js",function () {
    gulp.src("./src/js/**/*.js")
        .pipe(plumber())
        // .pipe(uglify())
        .pipe(rename({ suffix: ".min"}))
        .pipe(gulp.dest("./www/js/"));
});


gulp.task("sass",function () {   
    gulp.src([
        "./src/sass/**/*.scss",
        "!./src/sass/base.scss",
        "!./src/sass/index.scss",
    ])
        .pipe(plumber())
        .pipe(sass({outputStyle:"compact"}).on("error", sass.logError))
        .pipe(plumber())
        // .pipe(cleanCss({ compatibility:"ie8" })) //兼容性
        .pipe(rename({ suffix: ".min"}))
        .pipe(gulp.dest("./www/css/"));

    gulp.src([
        "./src/sass/base.scss",
        "./src/sass/tangth.scss",
    ])
        .pipe(plumber())
        .pipe(concat('tangth.scss'))
        .pipe(sass({outputStyle:"compact"}).on("error", sass.logError))
        .pipe(plumber())
        .pipe(cleanCss({ compatibility:"ie8" })) //兼容性
        .pipe(rename({ suffix: ".min"}))
        .pipe(gulp.dest("./www/css/"));
});

//监视器：html, css, js, sass
gulp.task("watch",function () {
    gulp.watch("./src/*.html",["html"]);
    gulp.watch("./src/css/**/*.css",["minify-css"]);
    gulp.watch("./src/js/**/*.js",["minify-js"]);
    gulp.watch("./src/sass/**/*.scss",["sass"]);
    gulp.watch("./www/**/*.*",["refresh"]);
});

//默认事务: copy, html,sass
// css, minify-css.
// js, minify-js,
// serve, watch
gulp.task("default", [
    "html", "minify-css",
    "minify-js","serve","sass", "watch"

]);





